package com.smart.community.property.controller;

import com.smart.community.commons.entity.property.PropertyCompany;
import com.smart.community.commons.dto.property.PropertyCompanyDTO;
import com.smart.community.property.service.IPropertyCompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户物业公司认证Feign控制器
 * 专门用于登录时的权限检查
 * 
 * @author Wu.Liang
 * @version 1.0.0
 * @since 2025-01-30
 */
@Tag(name = "用户物业公司认证Feign控制器", description = "用户物业公司认证相关Feign接口实现")
@RestController
@RequestMapping("/feign/user-property-company-auth")
@Slf4j
public class UserPropertyCompanyAuthFeignController {
    
    @Autowired
    private IPropertyCompanyService propertyCompanyService;
    
    /**
     * 根据物业公司ID获取物业公司信息
     * 
     * @param propertyCompanyId 物业公司ID
     * @return 物业公司信息
     */
    @Operation(summary = "根据物业公司ID获取物业公司信息")
    @GetMapping("/property-company/{propertyCompanyId}")
    public PropertyCompanyDTO getPropertyCompanyById(@Parameter(description = "物业公司ID") @PathVariable("propertyCompanyId") Long propertyCompanyId) throws Exception {
        log.info("Feign调用：获取物业公司信息，物业公司ID: {}", propertyCompanyId);

        PropertyCompany propertyCompany = propertyCompanyService.getById(propertyCompanyId);
        if (propertyCompany != null) {
            log.info("成功获取物业公司信息，物业公司ID: {}, 名称: {}", propertyCompanyId, propertyCompany.getCompanyName());
            PropertyCompanyDTO propertyCompanyDTO = convertToDTO(propertyCompany);
            return propertyCompanyDTO;
        } else {
            log.warn("物业公司不存在，物业公司ID: {}", propertyCompanyId);
            return new PropertyCompanyDTO();
        }
    }
    
    /**
     * 将PropertyCompany实体转换为PropertyCompanyDTO
     * 
     * @param entity PropertyCompany实体
     * @return PropertyCompanyDTO
     */
    private PropertyCompanyDTO convertToDTO(PropertyCompany entity) {
        if (entity == null) {
            return new PropertyCompanyDTO();
        }
        PropertyCompanyDTO dto = new PropertyCompanyDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }
} 